package com.woniu.atms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.woniu.atms.entity.*;
import com.woniu.atms.mapper.G5TicketDao;
import com.woniu.atms.mapper.G5TicketSeatMapper;
import com.woniu.atms.service.*;
import com.woniu.atms.vo.aft.ticket.AftPassengerTicketVo;
import com.woniu.atms.vo.aft.ticket.AfterTicketsVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class G5AftTicketServiceImpl extends ServiceImpl<G5TicketDao, G5Ticket> implements G5AftTicketService {
    @Resource
    private G5PassengerService g5PassengerService;
    @Resource
    private G5TicketSeatMapper g5TicketSeatMapper;
    @Resource
    private ScheduleService scheduleService;
    @Resource
    private RouteService routeService;
    @Resource
    private CarService carService;
    @Resource
    private CarSeatService carSeatService;
    @Override
    @Transactional(rollbackFor = {RuntimeException.class})
    public List<AfterTicketsVo> findTicket(G5Passenger g5Passenger) {
        //1.根据姓名身份证号查询乘车人id
        //2.根据乘车人id查询车票
        //3.根据车票上的时刻座位表查询车票座位中间表中的班次id
        //4.根据班次id查询班次
        //5。根据班次id查询班次中的路线id与车辆信息
        //6。根据路线id查询出路线，车辆信息
        List<AftPassengerTicketVo> aftPassengerTicketVos=new ArrayList<>();
        //1.查询乘车人
        QueryWrapper queryWrapper=new QueryWrapper();
        if(g5Passenger!=null){
            if(StringUtil.isNotEmpty(g5Passenger.getIdCard())){
                queryWrapper.eq("id_card",g5Passenger.getIdCard());
            }
            if(StringUtil.isNotEmpty(g5Passenger.getName())){
                queryWrapper.eq("name",g5Passenger.getName());
            }
        }
        List<G5Passenger> g5Passengers=g5PassengerService.list(queryWrapper);
        //2.根据乘车人id查询车票
        for (G5Passenger passenger:g5Passengers){
            //添加乘车人信息到vo中
            AftPassengerTicketVo aftPassengerTicketVo=new AftPassengerTicketVo();
            aftPassengerTicketVo.setG5Passenger(passenger);

            //查询该乘车人车票
            QueryWrapper queryWrapper1=new QueryWrapper();
            queryWrapper1.eq("passenger_id",passenger.getId());

            List<G5Ticket> g5Tickets=list(queryWrapper1);
            List<AfterTicketsVo> afterTicketsVos=new ArrayList<>();
            //3.
            for (G5Ticket g5Ticket:g5Tickets){

                AfterTicketsVo afterTicketsVo=new AfterTicketsVo();
                //设置车票
                afterTicketsVo.setG5Ticket(g5Ticket);
                //设置车票乘车人
                afterTicketsVo.setG5Passenger(passenger);
                //查车票座位表
                G5TicketSite g5TicketSite=g5TicketSeatMapper.selectById(g5Ticket.getTimeSeatId());
                //4.查座位表中的班次表,座位信息
                G5CarSeat g5CarSeat=carSeatService.queryById(g5TicketSite.getSiteId());
                afterTicketsVo.setG5CarSeat(g5CarSeat);

                G5Schedule g5Schedule=scheduleService.findById(g5TicketSite.getScheduleId());
                afterTicketsVo.setG5Schedule(g5Schedule);
                //5、6查班次表中的路线表
                G5Route g5Route=routeService.findRouteById(g5Schedule.getRouteId());
                afterTicketsVo.setG5Route(g5Route);
                G5Car g5Car=carService.queryById(g5Schedule.getCarId());
                afterTicketsVo.setG5Car(g5Car);
                afterTicketsVos.add(afterTicketsVo);
            }
            aftPassengerTicketVo.setAfterTicketsVos(afterTicketsVos);
            aftPassengerTicketVos.add(aftPassengerTicketVo);
        }

        //将查出导入信息存入单独的车票vo中
        List<AfterTicketsVo> afterTicketsVos=new ArrayList<>();

        for (AftPassengerTicketVo aftPassengerTicketVo:aftPassengerTicketVos){
            for (AfterTicketsVo afterTicketsVo:aftPassengerTicketVo.getAfterTicketsVos()){
                afterTicketsVos.add(afterTicketsVo);
            }
        }

        return afterTicketsVos;
    }
}
